update dm.dm_f101_round_f
set balance_out_rub =
	case
		when characteristic = 'A' and cur.currency_code in ('643', '810') then balance_in_rub - turn_cre_val + turn_deb_val
		when characteristic = 'P' and cur.currency_code in ('643', '810') then balance_in_rub + turn_cre_val - TURN_DEB_RUB
		else 0
	end,
	balance_out_val = 
	case
		when characteristic = 'A' and cur.currency_code not in ('643', '810') then balance_in_val - turn_cre_val + turn_deb_val
		when characteristic = 'P' and cur.currency_code not in ('643', '810') then balance_in_val + turn_cre_val - turn_deb_val
		else 0
	end
from 
	ds.md_account_d cur
where 
	cur.char_type = characteristic and
	substr(cur.account_number, 1, 5) = ledger_account;

update dm.dm_f101_round_f
set balance_out_total = balance_out_val + balance_out_rub;